﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class IRCForm : Form
    {
        protected IRC client;
        protected string incomingText;
        protected StreamWriter logger;
        protected Channel channel; // Only works for one channel atm

        public IRCForm()
        {
            InitializeComponent();
            this.ActiveControl = textBox2;
            channel = new Channel("", null);
            client = new IRC(this);
            logger = new StreamWriter("irc.log", true);
            logger.AutoFlush = true;
            incomingText = "";

            //listBox1.DoubleClick += OnListBoxItemClick;
            
            writeIncomingData("Welcome to the RuIRC client! " + Environment.NewLine + "For a list of commands type /commands");

        }
        
        //Delegate function so we can receive text from other threads.
        public delegate void UpdateTextCallback(string message);

        //Continuous loop which constantly asks for incoming text from the socket.
        private void loop()
        {
            while (true)
            {
                Thread.Sleep(2);
                if (client.getText(ref incomingText) > 0)
                {
                    textBox1.BeginInvoke(new UpdateTextCallback(writeIncomingData), new object[]{
                        incomingText});
                }
            }
        }

        //Writes incoming text to the textbox.
        public void writeIncomingData(string text)
        {
            textBox1.AppendText(text + Environment.NewLine);
            logger.WriteLine(DateTime.Now + ":"+text);

            //Stop the scrollbar from jumping up on new line
            textBox1.Select(textBox1.Text.Length - 1, 1);
            textBox1.ScrollToCaret();

        }

        //Detects the Enter keypress and sends the commands onwards for parsing.
        public void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (int)Keys.Enter)
            {
                //Send the input to the parser.
                client.sendParse(textBox2.Text, channel.getChannelName());
                //Reset the textbox to empty
                textBox2.Text = "";
            }
        }
 
        public void connectClient(string server, int port)
        {
            client.connect(server,port);
            Thread listener = new Thread(loop);
            listener.Start();
        }

        public void quit()
        {
            Application.Exit();
        }

        public void quitThread()
        {
            Application.ExitThread();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            client.sendParse(textBox2.Text, channel.getChannelName());
            textBox2.Text = "";
        }

        //Updates the listbox containing the users on a channel.
        public void AddToListBox(Channel channel)
        {
            this.channel = channel;
            if (InvokeRequired)
                BeginInvoke(new Action(() => AddToListBox(channel)));
            else
                listBox1.DataSource = channel.getUsernames();
        }

        public void cleanListBox()
        {
            if (InvokeRequired)
                BeginInvoke(new Action(() => cleanListBox()));
            else
            {
                listBox1.DataSource = null;
                listBox1.Items.Clear();
            }
        }

        private void OnListBoxItemClick(object sender, EventArgs e)
        {
            var form2 = new ConversationForm(listBox1.SelectedItem.ToString());
            form2.ShowDialog();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
